<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Models\Encyclopedias;
use App\Models\EncyclopediasCollect;
use App\Models\EncyclopediasLike;
use App\Models\EncyclopediasReply;
use App\Models\EncyclopediasReplyLike;
use Illuminate\Http\Request;

class WikiController extends Controller
{
    public function index(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Encyclopedias::with('types')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        foreach ($data as $k => $v){
            $data[$k]->type_name = '';
            if($v->types){
                $data[$k]->type_name = $v->types->name;
            }
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

    public function info(Request $Request)
    {
        $data = Encyclopedias::withCount('likes')->withCount('collections')->withCount('comments')->find($Request->id);
        $data->content = str_replace('src="/ueditor/php/upload','src="https://gl.xinzhuvip.com/ueditor/php/upload',$data->content);
        $data->comments;
        if($Request->uid){
            $isLikeId = [];
            $likes = EncyclopediasReplyLike::select('reply_id')->where('uid',$Request->uid)->get();
            foreach ($likes as $value) {
                $isLikeId[] = $value->reply_id;
            }
            foreach ($data->comments as $k => $v) {
                $data->comments[$k]->is_like = 0;
                if(in_array($v->id,$isLikeId)){
                    $data->comments[$k]->is_like = 1;
                }
            } 
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function like(Request $Request)
    {
        $data = EncyclopediasLike::where('interaction_id',$Request->id)->where('uid',$Request->uid)->first();
        if($data){
            $data->delete();
        }else{
            $data = new EncyclopediasLike();
            $data->interaction_id = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function collection(Request $Request)
    {
        $data = EncyclopediasCollect::where('interaction_id',$Request->id)->where('uid',$Request->uid)->first();
        if($data){
            $data->delete();
        }else{
            $data = new EncyclopediasCollect();
            $data->interaction_id = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function comment(Request $Request)
    {
        $data = new EncyclopediasReply();
        $data->user_id = $Request->uid;
        $data->content = $Request->content;
        $data->article_id = $Request->id;
        $data->to_uid = 0;
        $data->likes = 0;
        $data->review = 0;
        $data->r_id = 0;
        $data->create_time = time();
        $data->update_time = time();
        $data->save();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function commentLike(Request $Request)
    {
        $data = EncyclopediasReplyLike::where('reply_id',$Request->id)->where('uid',$Request->uid)->first();
        if($data){
            $data->delete();
        }else{
            $data = new EncyclopediasReplyLike();
            $data->reply_id = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
}
